﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using ComputerArchitecture.Core.Logging;
using ComputerArchitecture.Numbers;
using ComputerArchitecture.Units.Float;
using ComputerArchitecture.Units.Interfaces;

namespace ComputerArchitecture.Lab6
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Aleksey Suharev float-point number divide emulator 2013 @ p4s software group");
            Console.Write("Choose number format: 1 - float (32 bits); 2 - double (64 bits): ");
            Func<object, IFloatPointNumber> createNumber = null;
            Func<IFloatPointNumber, object> toNumber = null;
            Func<string, object> parseString = null;
            int format = 0;
            do
            {
                format = int.Parse(Console.ReadLine());
                if (format == 1)
                {
                    createNumber = d => ((float)d).ToFloatPointNumber();
                    toNumber = f => f.ToFloat();
                    parseString = s => float.Parse(s, NumberStyles.Float, CultureInfo.InvariantCulture);
                }
                if (format == 2)
                {
                    createNumber = d => ((double)d).ToDoublePointNumber();
                    toNumber = f => f.ToDouble();
                    parseString = s => double.Parse(s, NumberStyles.Float, CultureInfo.InvariantCulture);
                }
                if (format != 1 && format != 2)
                {
                    Console.WriteLine("Enter only 1 or 2!");
                }
            } while (format != 1 && format != 2);
            
            Console.Write("Enter first value: ");
            object first = parseString(Console.ReadLine());
            Console.Write("Enter second value: ");
            object second = parseString(Console.ReadLine());
            IFloatPointNumber firstNumber = createNumber(first), secondNumber = createNumber(second);


            ILoggingService log = new NLogFacadeLoggingService("logService");

            IFloatPointNumber result = null;
            IFloatDivideUnit divideUnit = new FloatDivideUnit(log);
            result = divideUnit.Divide(firstNumber, secondNumber);

            Console.WriteLine("Result: {0} ({1:f10})", result, toNumber(result));
        }
    }
}
